
<template>
  <!-- 工程切换弹窗 -->
  <div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      @row-click="rowClick"
    >
      <el-table-column width="55" type="selection"></el-table-column>
      <el-table-column prop="projectId" label="工程编号" align="center">
      </el-table-column>
      <el-table-column prop="projectName" label="工程名称" align="center">
      </el-table-column>
      <el-table-column prop="isUse" label="状态" sortable>
        <template slot-scope="scope">
          <div v-if="scope.row.isUse == 1">
            <i class="startState"></i><span>正在使用</span>
          </div>
          <div v-if="scope.row.isUse != 1">
            <i class="unStartState"></i><span style="color: red">未使用</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="saveFunc">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </span>
  </div>
</template>

<script>
import { systemManage } from "@/api/index";
export default {
  data() {
    return {
      tableData: [],
      searchForm: {
        page: 1,
        size: 15,
        projectName: "",
      },
    };
  },
  mounted() {
    this.projectQueryForPage();
  },
  methods: {
    searchClick() {
      this.searchForm.page = 1;
      this.projectQueryForPage();
    },
    rowClick(row) {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection();
      }
      this.$refs.multipleTable.toggleRowSelection(row, true);
    },
    cancel() {
      this.$emit("visibleEngineerCancel", false);
    },
    saveFunc() {
      let selection = this.$refs.multipleTable.selection;
      if (selection.length != 1) {
        return this.$message.warning("请选择一条数据");
      }
      this.$emit("visibleEngineerSave", false);
    },
    // 获取所有租户
    projectQueryForPage() {
      systemManage.getMgrProjects().then((res) => {
        if (res.code == 200) {
          if (!res.datas) {
            return this.$message.error(res.message || "请联系管理员配置工程");
          }
          this.tableData = res.datas.data;
          if (res.datas.isAdmin == true) {
            console.log("系统管理员");
            return this.$message.error("该用户为系统管理员");
          } else {
            if (res.datas.data.length == 0) {
              return this.$message.error("请联系管理员配置工程");
            } else {
              this.$nextTick(() => {
                this.$refs.multipleTable.clearSelection();
                for (let i = 0; i < res.datas.data.length; i++) {
                  if (res.datas.data[i].isUse == "1") {
                    console.log(
                      this.$refs.multipleTable,
                      "this.$refs.multipleTable999998"
                    );
                    this.$refs.multipleTable.toggleRowSelection(
                      this.$refs.multipleTable.data[i],
                      true
                    );
                    break;
                  }
                }
              });
              this.tenantVisible = true;
            }
          }
        } else {
          this.$message.error(res.message);
        }
      });
    },
  },
};
</script>

<style style="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: center;
  padding: 16px;
}
</style>